﻿
#pragma once

#include "WorkerModel.h"

#include <QObject>
#include <QMessageBox>

struct Worker {
    WorkerModel::WebdavCfg Cfg;
    WorkerModel *Model;
    QThread *Thread;

    Worker() {
        Model = nullptr;
        Thread = nullptr;
    }
};

class Model : public QObject
{
    Q_OBJECT
public:
    explicit Model(QObject *parent = nullptr);
    ~Model();

    const WorkerModel::WebdavCfg &GetWebdavCfg(int workerIndex);
    int GetWebdavCfgCount();
    void AsyncSetWebdavCfg(int workerIndex, const WorkerModel::WebdavCfg &cfg);
    void AddWorker(const WorkerModel::WebdavCfg &cfg);
    void RemoveWorker(int workerIndex);

private Q_SLOTS:
    void onRecvError(WorkerModel::Error err);
    void onSetWebdavCfgFinished(bool succeed, const WorkerModel::WebdavCfg &cfg);
    void onWorkerStateChanged(WorkerModel::State s);

Q_SIGNALS:
    void ReqThreadSetWebdavCfg(int workerModelId, const WorkerModel::WebdavCfg &cfg);
    void SetWebdavCfgFinished(int workerIndex, bool succeed, const WorkerModel::WebdavCfg &cfg);
    void WorkerStateChanged(int workerIndex, WorkerModel::State s);

private:
    void loadConfig();
    void saveCfg();
    Worker createWorker(const WorkerModel::WebdavCfg &cfg);

    int findWorkerIndexByWorkerModel(const WorkerModel *model);

private:
    QString m_cfgFilePath;
    int m_workerModelId;
    QList<Worker> m_workerList;
};
